package com.hzjh.h5mall.ui.register

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.widget.dialog.JHMnemonicCheckSuccessDialog
import com.hzjh.base.frame.adapter.MutableAdapter
import com.hzjh.base.frame.adapter.SimpleGapItemDecor
import com.hzjh.base.utils.function.dp2px
import com.hzjh.base.utils.other.ActivityStackManage
import com.hzjh.h5mall.adapter.register.MnemonicAdapter
import com.hzjh.h5mall.bean.register.MnemonicWordBean
import com.hzjh.h5mall.databinding.ActivityMnemonicCheckBinding
import com.hzjh.h5mall.databinding.ItemMnemonicBinding
import com.hzjh.h5mall.model.register.RegisterVM

//助记词校验
@Route(path = RouteUrl.MNEMONIC_CHECK_ACTIVITY)
class MnemonicCheckActivity : JHBaseActivity<ActivityMnemonicCheckBinding, RegisterVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_EMAIL)
    var email = ""

    @JvmField
    @Autowired(name = RouteUrl.PARAMS_MNEMONIC_WORD)
    var mMnemonicWord : Bundle?=null

    private val  mnemonicAdapter by lazy {
        MutableAdapter(viewModel.mnemonicList.value!!).apply{
            addVhDelegate {
                MnemonicAdapter(ItemMnemonicBinding.inflate(layoutInflater, it, false),1){model,word->
                    model.name=word
                    checkInputWord()
                }
            }
        }
    }
    override fun initData() {
        mMnemonicWord?.let {
            viewModel.randomMnemonic(it.getSerializable("mnemonicList") as ArrayList<MnemonicWordBean>)
        }
    }

    override fun initView() {
        viewBinding.rvMnemonic.layoutManager = GridLayoutManager(this,3)
        viewBinding.rvMnemonic.addItemDecoration(
            SimpleGapItemDecor( verGap = 12.dp2px())
        )
        viewBinding.rvMnemonic.adapter = mnemonicAdapter

        viewBinding.btnMnemonic.setOnClickListener {
            val key = viewModel.mnemonicList.value?.joinToString(" ") { it.name }
            viewModel.reqVerifyMetamaskLogin(email,key?:"")
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.mnemonicList.observe(this){
            mnemonicAdapter.notifyDataSetChanged()
        }
        viewModel.mnemonicSuccess.observe(this){
            if(it){
                //校验成功
                JHMnemonicCheckSuccessDialog.Builder(ActivityStackManage.currentActivity())
                    .setCanCancel(false)
                    .setCanceledOnTouchOutside(false)
                    .setClickIKnowLinstener {
                        //验证成功点击I know跳转首页
                        ActivityStackManage.toMainActivity()
                    }
                    .create().show()
            }
        }
    }

    private fun checkInputWord(){
        //校验是否填写完成
        viewBinding.btnMnemonic.isEnabled = viewModel.mnemonicList.value!!.all { it.name.isNotBlank() }
    }

    override fun initViewBinding(): ActivityMnemonicCheckBinding {
        return ActivityMnemonicCheckBinding.inflate(layoutInflater)
    }
}